<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<script type="text/javascript">
	function confirmDelete(id){
		if (confirm('are you sure you want to delete this post?')){
			window.location = "./view.m?page=post-update&action=del&id=" + id;
		}
	}
</script>

<c:set var="postExist" value="${postExist}" scope="request"/>
<% 
boolean postExist = (Boolean)request.getAttribute("postExist");
if (!postExist){
%>

<table cellpadding="3" cellspacing="1" border="0" width="100%" height=500px>
	<tr class="row-title"><td class="header21" align=left>
		&nbsp;<b>error:</b>
	</td></tr>
	<tr><td valign="top" align="center">

		<table cellpadding="0" cellspacing="1" border="0" bgcolor="#4682B4"  width="350"><tr><td>
		<table cellpadding="2" cellspacing="0" border="0" width="350" bgcolor="#DBEAF5">
			<tr><td colspan="2">&nbsp;</td></tr>
			<tr><td align="center">
				<font class="button"><c:out escapeXml="false" value="${message}"/></font>
			</td></tr>
			<tr><td colspan="2">&nbsp;</td></tr>
			</table>
		</table><br>
		
	</td></tr>
</table>

<% }else{ %>			

<table cellpadding="3" cellspacing="1" border="0" width="100%" height=500px>
	<tr class="row-title"><td class="header21" align=left>
		<a href="view.m?page=posts">posts</a>
		->&nbsp;<a href="view.m?page=posts-cat&id=<c:out value="${post.category.id}"/>"><c:out value="${post.category.name}"/></a>
	</td></tr>
	<tr><td valign="top">
	
		<table border="0" width=100% cellpadding="2" cellspacing="1">
			<tr class="row-title"><td width=485px align=left class="post-title">
				<b><c:out escapeXml="false" value="${post.title}"/></b>
			</td><td align=right class="post-title">
				<fmt:formatDate value="${post.dateTime}" type="both" />
			</td></tr>
			
			<c:set var="postImageUrl" value="${post.imageUrl}" scope="request"/>
			<% 
			String postImageUrl = (String)request.getAttribute("postImageUrl");
			if(postImageUrl == null || postImageUrl.trim().equals("")){ 
			%>
			<tr><td colspan="2" class="post-summary" align=left>
				<c:out escapeXml="false" value="${post.summary}"/>
			</td></tr>
			<%}else{ %>
			<tr><td align=left valign="top" colspan="2">
			<table width=100% cellpadding="2" cellspacing="1">
				<tr><td align=left width=1px>
					<img src="<c:out value="${post.imageUrl}" />" alt="<c:out value="${post.title}"/>" class="image"/>
				</td><td align=left valign="top" class="post-summary">
					<c:out escapeXml="false" value="${post.summary}" />
				</td></tr>
			</table>
			</td></tr>
			<%} %>
			
			<tr><td colspan=2>
				<hr color="grey" width="100%" size="1"/> 
			</td></tr>
			<tr><td colspan="2" align=left>
				<c:out escapeXml="false" value="${post.content}"/>
				<br><br>
			</td></tr>
			<% if (session.getAttribute("author") != null){ %>
			<tr><td align="left">
				<a href="user.m?id=<c:out value="${post.author.id}"/>"><c:out value="${post.author.fullname}"/></a> 
				<br>
			</td><td align=right>
				<a href="./view.m?page=post-update&action=edit&id=<c:out value="${post.id}"/>">
					<img src="./images/common/pencil.png" title="edit this post" width="20px" border="0"/>
				</a>&nbsp;
				<input type="image" 
					src="./images/common/edit_delete.png" 
					title="delete this post" 
					onclick="confirmDelete(<c:out value="${post.id}"/>);"
					width="20px" />
			</td></tr>
			<% }else{ %>
			<tr><td colspan="2" align="left">
				<a href="user.m?id=<c:out value="${post.author.id}"/>"><c:out value="${post.author.fullname}"/></a> 
				<br>
			</td></tr>
			<% } %>
			<tr><td colspan="2">
				<hr color="grey" width="100%" size="1"/> 
			</td></tr>
		</table>
		
	</td></tr>
</table>

<% } %>